/*
 * @Description: 档案鉴定管理上下文
 * @Author: Rfan
 * @Date: 2022-07-29 16:18:02
 * @LastEditTime: 2022-07-29 19:22:12
 */

import { createContext, useContext, useEffect, useReducer } from 'react';
import { message, Modal } from 'antd';
import {
  SET_BATCH_LIST,
  DELETE_BATCH_ITEM,
  SET_SELECTED_ITEM,
  ADD_BATCH_ITEM,
  SET_SELECT_AT,
  SET_SELECT_AT_ID,
  SET_SELECT_FOND,
  SET_SELECT_FOND_ID,
  EDIT_BATCH_ITEM
} from '../constants';
import { AppraisalMgtReducer } from '../reducers/AppraisalMgtReducer';
import {
  addPendingBatch,
  deletePendingDispositionBatch,
  editPendingBatch,
  fetchPendingBatchListByPage
} from '@/api/archives/disposition';

const initialState = {
  // 当前选中的项目
  selectFond: {},
  selectAT: {},
  selectFondId: 0,
  selectATId: 0,
  // 选中的表格条目
  selectItems: [],

  // 导出列表
  batchList: [],

  // -------- 字典 --------

  // 批次状态列表
  stateList: [
    { id: 0, name: '待导出', color: 'blue' },
    { id: 1, name: '导出中', color: 'blue' },
    { id: 2, name: '导出完成', color: 'green' },
    { id: 3, name: '导出失败', color: 'red' }
  ]
};

const AppraisalMgtContext = createContext();

const AppraisalMgtContextProvider = (props) => {
  const [state, dispatch] = useReducer(AppraisalMgtReducer, initialState);

  const actions = {
    /**
     * @description: 设置选中的全宗
     */
    setSelectFond(data) {
      dispatch({ type: SET_SELECT_FOND, data });
    },

    /**
     * @description: 设置选中的档案类型
     */
    setSelectAT(data) {
      dispatch({ type: SET_SELECT_AT, data });
    },

    /**
     * @description: 设置选中的全宗
     */
    setSelectFondId(id) {
      dispatch({ type: SET_SELECT_FOND_ID, id });
    },

    /**
     * @description: 设置选中的档案类型ID
     */
    setSelectATId(id) {
      dispatch({ type: SET_SELECT_AT_ID, id });
    },

    /**
     * @description: 设置表格中选中条目的key
     * @param {*} selectArr
     * @return {*}
     */
    setSelectKey(selectArr) {
      dispatch({ type: SET_SELECTED_ITEM, data: selectArr });
    },

    /**
     * @description: 鉴定批次列表方法
     * @param {IExportListParams} params 参数
     * @return {Promise}
     */
    getBatchListFunc(params) {
      return new Promise((resolve, reject) => {
        fetchPendingBatchListByPage(params)
          .then((res) => {
            const { data } = res;
            dispatch({ type: SET_BATCH_LIST, data: data.results });
            resolve(data);
          })
          .catch((err) => {
            reject(err);
          });
      });
    },

    /**
     * @description: 鉴定批次列表方法
     * @param {IExportListParams} params 参数
     * @return {Promise}
     */
    addBatchFun(params) {
      return new Promise((resolve, reject) => {
        addPendingBatch(params)
          .then((res) => {
            message.success('增加鉴定批次成功');
            const { data } = res;
            dispatch({ type: ADD_BATCH_ITEM, data });
            resolve(data);
          })
          .catch((err) => {
            reject(err);
          });
      });
    },

    /**
     * @description: 鉴定批次列表方法
     * @param {IExportListParams} params 参数
     * @return {Promise}
     */
    editBatchFun(params) {
      return new Promise((resolve, reject) => {
        editPendingBatch(params)
          .then((res) => {
            message.success('编辑鉴定批次成功');
            const { data } = res;
            dispatch({ type: EDIT_BATCH_ITEM, data, id: data.id });
            resolve(data);
          })
          .catch((err) => {
            reject(err);
          });
      });
    },

    /**
     * @description: 删除批次条目
     * @param {number|number[]} id
     * @param {string} title
     */
    deleteBatchFunc(id, title = '确认删除选中的批次？') {
      Modal.confirm({
        title: '删除',
        content: title,
        onOk() {
          const ids = id instanceof Array ? id.join(',') : id;
          deletePendingDispositionBatch(ids).then((res) => {
            message.success('删除成功');
            const data = res.data.map((item) => parseInt(item, 10));
            dispatch({ type: DELETE_BATCH_ITEM, ids: data });
          });
        },
        onCancel() {
          console.log('Cancel');
        }
      });
    }
  };

  useEffect(() => {}, []);

  return (
    <AppraisalMgtContext.Provider value={{ ...state, ...actions }}>
      {props.children}
    </AppraisalMgtContext.Provider>
  );
};

export const useAppraisalMgtContext = () => {
  return useContext(AppraisalMgtContext);
};

export { AppraisalMgtContext, AppraisalMgtContextProvider };
